package lyl.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

import lyl.file.FileUtil;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;



/**
 * 
 * @author Liu Yulong
 */
public class XmlTest {

    /**
     * @param args
     */
    public static void main(String[] args){
        try {
            test2();
        }
        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void test2() throws FileNotFoundException, DocumentException{
        SAXReader reader = new SAXReader();
        Document doc = reader.read(new FileInputStream("d:/formData.xml"));
        List nodes = doc.selectNodes("/data/*");
        for(int i = 0;i < nodes.size();i++){
            org.dom4j.Node node = (org.dom4j.Node)nodes.get(i);
            System.out.println("name------------"+node.getName());
            System.out.println("value------------"+node.getText());
        }
    }
    
    public static void test1(){
        Document document = DocumentHelper.createDocument();
        Element element = DocumentHelper.createElement("test");
        try{
            byte[] bytes = FileUtil.fileToStream(new File("d:/01.jpg"));
            document.setXMLEncoding("ISO-8859-1");
            element.add(DocumentHelper.createCDATA(new String(bytes,"ISO-8859-1")));
            //FileUtil.saveFile("d:/ddds.jpg", (new String(bytes,"ISO-8859-1")).getBytes("ISO-8859-1"));
            document.add(element);
            FileUtil.saveFile("d:/ddd.xml", document.asXML());
            
            document = DocumentHelper.parseText(new String(FileUtil.fileToStream(new File("d:/ddd.xml")),"ISO-8859-1"));
           //System.out.println(document.getRootElement().getData()); 
           FileUtil.saveFile("d:/ddd.jpg", document.getRootElement().getData().toString().getBytes("ISO-8859-1"));
        }catch(Exception e){e.printStackTrace();}
    }
}
